﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy1_10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 校验操作数是否为无符号整数
        private bool IsValidUnsignedInt(string input)
        {
            return Regex.IsMatch(input, @"^\d+$");
        }

        // 加法按钮
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (ValidateInputs())
            {
                int num1 = int.Parse(textBox1.Text);
                int num2 = int.Parse(textBox2.Text);
                textBox3.Text = (num1 + num2).ToString();
            }
        }

        // 减法按钮
        private void buttonSub_Click(object sender, EventArgs e)
        {
            if (ValidateInputs())
            {
                int num1 = int.Parse(textBox1.Text);
                int num2 = int.Parse(textBox2.Text);
                textBox3.Text = (num1 - num2).ToString();
            }
        }

        // 乘法按钮
        private void buttonMul_Click(object sender, EventArgs e)
        {
            if (ValidateInputs())
            {
                int num1 = int.Parse(textBox1.Text);
                int num2 = int.Parse(textBox2.Text);
                textBox3.Text = (num1 * num2).ToString();
            }
        }

        // 除法按钮
        private void buttonDiv_Click(object sender, EventArgs e)
        {
            if (ValidateInputs())
            {
                int num1 = int.Parse(textBox1.Text);
                int num2 = int.Parse(textBox2.Text);
                if (num2 == 0)
                {
                    MessageBox.Show("除数不能为零！", "错误");
                    return;
                }
                textBox3.Text = (num1 / (double)num2).ToString();
            }
        }

        // 清空按钮
        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
        }

        // 退出按钮
        private void buttonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // 文本框离开事件：校验输入格式
        private void textBox1_Leave(object sender, EventArgs e)
        {
            ValidateSingleInput(textBox1);
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            ValidateSingleInput(textBox2);
        }

        // 校验单个文本框输入
        private void ValidateSingleInput(TextBox txt)
        {
            if (string.IsNullOrEmpty(txt.Text))
            {
                MessageBox.Show("操作数不能为空！", "错误");
                txt.Focus();
            }
            else if (!IsValidUnsignedInt(txt.Text))
            {
                MessageBox.Show("只能输入无符号整数！", "错误");
                txt.SelectAll();
                txt.Focus();
            }
        }

        // 校验所有输入
        private bool ValidateInputs()
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("操作数不能为空！", "错误");
                return false;
            }
            if (!IsValidUnsignedInt(textBox1.Text) || !IsValidUnsignedInt(textBox2.Text))
            {
                MessageBox.Show("只能输入无符号整数！", "错误");
                return false;
            }
            return true;
        }
    }
}
